package com.shop.utils;

import java.security.MessageDigest;
import java.util.Random;

public class MD5Utils {
	
	/**
	 * 生成 6位数 字符串
	 * @return
	 */
	public static String encry(){
		char[] attr = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
		char[] resultStr = new char[6];
		for(int i=0;i<6;i++){
			int temp = new Random().nextInt(60);
			resultStr[i] = attr[temp];
		}
		
		return new String(resultStr);
	}
	/**
	 * 传入密码和加密串，生成我们想要的md5密码串
	 */
	public static String makeMD5Password(String password, String uencry) {
		String md5Password = null;
		try {
			md5Password = MD5(MD5(password)+uencry);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return md5Password;
	}

	/**
	 * 传入密码生成MD5加密后的密码
	 */
	public static String MD5(String password) throws Exception {
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(password.getBytes());
		byte[] encodedPassword = md.digest();

		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < encodedPassword.length; i++) {
			if ((encodedPassword[i] & 0xff) < 0x10) {
				sb.append("0");
			}
			sb.append(Long.toString(encodedPassword[i] & 0xff, 16));
		}
		return sb.toString();
	}

/*	public static void main(String[] args) throws NoSuchAlgorithmException {
		String md5Password = makeMD5Password("1weadfas", "ajLik");
		System.out.println(md5Password);
	}*/
}
